import hashlib
import os
from random import random, choice, randint
import re


# 持久化保存：文件
# list  元组   字典-----》内存

# 登录后主页界面
#
# def home_page():
#     while True:
#         tourist()
#         user()


# 用户登录
def user():
    print('************欢迎来到广东工业大学图书馆************')
    print('''
                 1.浏览图书馆书籍
                 2.查看已借书籍
                 3.还书籍
                 4.退出当前账户
        ''')
    want = input('输入想要进行操作的数字:')
    if want == '1':
        borrow_book()
    elif want == '2':
        show_books()
    elif want == '3':
        give_back()
    elif want == '4':
        tourist()
    else:
        print('请重新选择上面的选项!')
        print('''
                 00.账号登陆
                 0.新用户注册
                 4.退出当前账户
            ''')
        want = input('输入想要进行操作的数字:')


# 游客登录
def tourist():
    global login_state
    while True:
        print('游客,登陆后才能访问图书馆!')
        print('''
                         00.账号登陆
                         0.新用户注册
                                 ''')
        want = input('输入想要进行操作的数字:')
        if want == '00':
            login()
            return user()

        elif want == '0':
            register()
            return user()

        else:
            print('请重新选择上面的选项!')
            print('''
                     00.账号登陆
                     0.新用户注册
                ''')
            want = input('输入想要进行操作的数字:')


# 新用户注册：
def register():
    print('--------------------用户注册界面------------------')
    while True:
        username = input('用户名：')
        password = input('密码(数字组合共6个字符以上)：')
        repassword = input('确认密码：')
        with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user.txt', 'r')as rstream:
            users = rstream.readlines()
            print('----users----', users)
            for user in users:
                result_user = re.match(r'(\w+?) (\w+?)', user)
                result_password = re.match(r'\d{6}', password)
            if username == result_user.group(1):
                print('用户名存在,请重新命名')
                continue
            elif password != repassword or result_password == None:
                print('密码不一致！/密码低于六个数字')
                continue
            else:
                # 密码加密
                def securety():
                    password_safe = hashlib.md5(password.encode('utf-8'))
                    password_safe = password_safe.hexdigest()
                    # 保存信息
                    with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user.txt',
                              'a+') as wstream:
                        wstream.write('\n{} {}'.format(username, password_safe))
                        print('用户注册成功')
                        print('用户注册成功')
                        print('用户注册成功')
                        print('欢迎{}用户,充分利用图书馆资源吧!'.format(username))

                securety()
        break


# 登录随机验证码
def login_check():
    while True:
        code = ''
        for i in range(4):
            c = choice([str(randint(0, 9)), chr(randint(65, 90)), chr(randint(97, 122))])
            code += c
        print(code)
        input_code = input('输入所示验证码:')
        if input_code == code:
            break
        else:
            print('验证码错误!')


# 用户登录
def login():
    print('--------------------用户登录界面------------------')
    while True:
        username = input('用户名：')
        password = input('密码：')
        password_safe = hashlib.md5(password.encode('utf-8'))
        password_safe = password_safe.hexdigest()
        login_check()
        if username and password:
            with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user.txt', 'r')as rsteam:
                user = rsteam.read()
                input_name = '{} {}'.format(username, password_safe)
                if input_name in user:
                    print('用户登陆成功')
                    break
                else:
                    print('用户名/密码输入错误！')
                    continue
        else:
            print('!!!!!!!!!!!!!!!!!!请输入账户密码!!!!!!!!!!!!!!!!!!!')


# 返回主页界面
def back_home():
    number = input('\n输入out,返回主页!')
    while True:
        if number == 'out':
            user()
            break
        else:
            print('\n无效键,请重新输入!')
            number = input('\n输入out,返回主页!')


# 图书馆浏览书籍/借书
def borrow_book():
    print('--------------------图书管书籍总目录------------------')
    with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\lib_books.txt', 'r',encoding='utf-8')as rsteam:
        books = rsteam.read()
        print(books)
        b_book = input('\n请输入想要借书籍的名称:')
        with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user_books.txt', 'a+',encoding='gbk')as asteam:
            user_books = asteam.read()
            if b_book not in user_books and b_book in books:
                asteam.write(b_book)
                print('成功借走此书')
            elif b_book not in user_books:
                print('此书已经在您的口袋中')
            else:
                print('无此书，请重新输入')
        state = input('是否继续进行借书籍(y/n):')
        if state == 'y':
            borrow_book()
        else:
            back_home()


# 用户查看书籍界面
def show_books():
    print('--------------------用户已借书籍记录------------------')
    with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user_books.txt', 'r',encoding='gbk')as rsteam:
        books = rsteam.read()
        book_count = 0
        for book in books:
            print(book, end='')
            book_count+=1
        print('您借的书共计：{}本'.format(book_count))
    back_home()


# 归还书籍
def give_back():
    with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user_books.txt', 'r+')as rsteam:
        books = rsteam.readlines()
        print(books)
        back_book = input('请输入归还书籍名称:')
        with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user_books.txt', 'a')as wsteam:
            book_count = 0
            for book in books:
                # print(book, end='')
                if back_book in book:
                    continue
                wsteam.write(book)
        with open(r'C:\Users\123\PycharmProjects\rasan-python\Learn Knowledge\图书管理系统\user_books.txt', 'r')as rsteam:
            books = rsteam.readlines()
            book_count = 0
            for book in books:
                # print(book, end='')
                if back_book in book:
                    book_count+=1
            print('尚未归还的书籍:', books)
            print('您借的书共计：{}本'.format(book_count))
    state = input('是否继续进行归还书籍(y/n):')
    if state == 'y':
        give_back()
    else:
        back_home()


# 调用函数
# home_page()
tourist()
